package com.qf.sl.youshare.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.qf.sl.youshare.R;
import com.qf.sl.youshare.model.User;
import com.qf.sl.youshare.model.UserLogin;
import com.qf.sl.youshare.tasks.TaskProcessor;
import com.qf.sl.youshare.tasks.UserLoginTask;
import com.qf.sl.youshare.tasks.UserRegisterTask;
import com.qf.sl.youshare.utils.HttpTools;
import com.qf.sl.youshare.utils.UserCurrent;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

/**
 * Created by IDEA
 * User : SL
 * on  2015/2/11.
 * 22:54
 */
public class UserRegisterActivity extends Activity implements View.OnClickListener, TaskProcessor {

    private EditText registerEmail;
    private EditText registerPassword;
    private EditText registerPasswordConfirm;
    private String email;
    private String password;
    private String passwordConfirm;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_register);

        registerEmail = (EditText) findViewById(R.id.text_register_user_email);
        registerPassword = (EditText) findViewById(R.id.text_register_user_password);
        registerPasswordConfirm = (EditText) findViewById(R.id.text_register_user_password_confirm);
        Button registerRegister = (Button) findViewById(R.id.btn_register_register);

        registerRegister.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        email = registerEmail.getText().toString();
        if (!email.equals("") && email.length() > 0){
            if (email.contains("@") && email.indexOf("@") < email.indexOf(".com")){
                password = registerPassword.getText().toString();
                passwordConfirm = registerPasswordConfirm.getText().toString();

                if (!password.equals("") && password.length() > 0){
                    if (password.length() > 6 && password.length() < 13){
                        if (password.equals(passwordConfirm)){
                            if (id == R.id.btn_register_register){
                                //TODO 用户注册

                                UserRegisterTask task = new UserRegisterTask(this);
                                task.execute(email, password, passwordConfirm);

//                                String url = Constants.USER_REGISTER_URL +
//                                            "user=" + email +
//                                            "&password=" + password +
//                                            "conform_password=" + passwordConfirm;
//                                byte[] bytes = HttpTools.get(url);

//                                HashMap<String, String> params = new HashMap<String, String>();
////                                params.put("ac","register");
//                                params.put("user",email);
//                                params.put("password",password);
//                                params.put("conform_password", passwordConfirm);
//
//                                byte[] bytes = HttpTools.post(Constants.USER_URL + "?ac=register", params, "UTF-8");
//
//                                try {
//
//                                    String string = new String(bytes, "UTF-8");
//                                    JSONObject object = new JSONObject(string);
//                                    int code = object.getInt("code");
//                                    if (code == 1){
//                                        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
//                                        UserCurrent userCurrent = UserCurrent.getInstance();
//
//                                        User user = new User();
//                                        user.setName(email);
//                                        user.setPassword(password);
//                                        userCurrent.setUser(user);
//
//                                        params = null;
//                                        params = new HashMap<String,String>();
//                                        params.put("ac","login");
//                                        params.put("user",email);
//                                        params.put("password",password);
//
//                                        byte[] bytesLogin = HttpTools.post(Constants.USER_URL, params, "UTF-8");
//
//                                        try {
//                                            String stringLogin = new String(bytesLogin, "UTF-8");
//                                            JSONObject jsonObject = new JSONObject(stringLogin);
//                                            int codeLogin = jsonObject.getInt("code");
//                                            if (codeLogin == 1){
//                                                JSONObject msg = jsonObject.getJSONObject("msg");
//
//                                                //TODO 记录当前登录用户
//                                                UserLogin userLogin = userCurrent.getUserLogin();
//                                                if (userLogin != null) {
//                                                    userLogin.parseJson(msg);
//                                                }
//                                                userCurrent.setUserLogin(userLogin);
//
//                                                finish();
//
//                                            }else {
//                                                String msg = jsonObject.getString("msg");
//                                                Toast.makeText(this, "登录失败:" + msg, Toast.LENGTH_SHORT).show();
//                                            }
//                                        } catch (UnsupportedEncodingException e) {
//                                            e.printStackTrace();
//                                        } catch (JSONException e) {
//                                            e.printStackTrace();
//                                        }
//
//                                    }else {
//                                        Toast.makeText(this, "注册失败,请重新填写注册信息", Toast.LENGTH_SHORT).show();
//                                    }
//
//                                } catch (UnsupportedEncodingException e) {
//                                    e.printStackTrace();
//                                } catch (JSONException e) {
//                                    e.printStackTrace();
//                                }

                            }
                        }
                    }else {
                        Toast.makeText(this, "密码长度不正确", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(this, "密码格式不能为空", Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(this, "邮箱格式不正确", Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(this, "邮箱不能为空", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void processResult(JSONObject jsonObject) {
        try {
            int code = jsonObject.getInt("code");
            if (code == 1){

                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                final UserCurrent userCurrent = UserCurrent.getInstance();

                User user = new User();
                user.setName(email);
                user.setPassword(password);
                userCurrent.setUser(user);


                UserLoginTask task = new UserLoginTask(new TaskProcessor() {
                    @Override
                    public void processResult(JSONObject jsonObject) {
                        UserLogin login = new UserLogin();

                        try {
                            int loginCode = jsonObject.getInt("code");
                            if (loginCode == 1){
                                JSONObject msg = jsonObject.getJSONObject("msg");
                                login.parseJson(msg);
                                userCurrent.setUserLogin(login);
                                finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                task.execute(email, password);

            } else {
                Toast.makeText(this, "注册失败,请重新填写注册信息", Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}